package net.vz.geoapi;

import java.util.HashMap;
import java.util.Map;

import net.vz.geoapi.utils.GeoApiInputException;

/**
 * search with a latitude/longitude coordinate pair and an optional radius distance
 * 
 * @author sirrgang
 *
 */
public class SimpleSearch extends GeneralSearch 
{
    private static String URL_SEARCH_METHOD = "search";
    private static String RADIUS_METRIC_KM     = "km";
    
    private Double longitude = null;
    private Double latitude = null;
    private float radius;
    
    
    public SimpleSearch(String _apiKey, String _version) {
        super(_apiKey, _version);
    }
    
    public SimpleSearch(String _apiKey) {
        super(_apiKey, DEFAULT_VERSION);
    }
    
    public void setLongitude(double _lon) {
        this.longitude = _lon;
    }
    
    public void setLatitude(double _lat) {
        this.latitude = _lat;
    }
    
    /**
     * set radius in km
     * @param _d
     */
    public void setRadius(float _d) {
        this.radius = _d;
    }
    
    @Override
    protected void validateInput() throws GeoApiInputException {
        super.validateInput();
        
        if (this.longitude == null || this.latitude == null || this.getLimit() == 0) {
            throw new GeoApiInputException();
        }
    }
    
    @Override
    protected String getSearchMethod() {
        return URL_SEARCH_METHOD;
    }
    
    @Override
    protected Map<String, String> getRequestParams() {
        HashMap<String, String> map = new HashMap<String, String>();
        
        map.put("lat", String.valueOf(this.latitude));
        map.put("lon", String.valueOf(this.longitude));
        map.put("radius", String.valueOf(this.radius) + RADIUS_METRIC_KM);
        if (this.getType() != null) {
            map.put("type", this.getType());
        }
        return map;
    }
}
